package com.ss.android.article.base.utils.b;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.ui.j;
import com.ss.android.article.browser.R;

import java.lang.ref.WeakReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class LinkSpan extends ClickableSpan {
    public static final class a extends LinkMovementMethod {
        private ClickableSpan a;
        private boolean b;
        private Object c;
        private int d;

        public a(Context arg3) {
            this.d = arg3.getResources().getColor(com.ss.android.article.browser.R.color.fh);
        }

        private static ClickableSpan a(TextView arg3, Spannable arg4, MotionEvent arg5) {
            Object v0_2;
            int v0 = ((int) arg5.getX());
            int v1 = ((int) arg5.getY());
            v0 -= arg3.getTotalPaddingLeft();
            v1 -= arg3.getTotalPaddingTop();
            v0 += arg3.getScrollX();
            v1 += arg3.getScrollY();
            Layout v2 = arg3.getLayout();
            v0 = v2.getOffsetForHorizontal(v2.getLineForVertical(v1), ((float) v0));
            Object[] v0_1 = arg4.getSpans(v0, v0, ClickableSpan.class);
            ClickableSpan v1_1 = null;
            if (v0_1.length > 0) {
                v0_2 = v0_1[0];
            } else {
                v0_2 = v1_1;
            }

            return ((ClickableSpan) v0_2);
        }

        public final boolean onTouchEvent(@NonNull TextView arg5, @NonNull Spannable arg6, @NonNull MotionEvent arg7) {
            Object v2 = null;
            if (arg7.getAction() == 0) {
                this.a = a(arg5, arg6, arg7);
                if (this.a != null) {
                    if (this.c != null) {
                        arg6.removeSpan(this.c);
                    }

                    this.c = new BackgroundColorSpan(this.d);
                    arg6.setSpan(this.c, arg6.getSpanStart(this.a), arg6.getSpanEnd(this.a), 33);
                    Selection.setSelection(arg6, arg6.getSpanStart(this.a), arg6.getSpanEnd(this.a));
                    this.b = true;
                } else {
                    this.b = false;
                }
            } else {
                if (arg7.getAction() == 2) {
                    ClickableSpan v0 = a(arg5, arg6, arg7);
                    if (this.a == null) {
                        return this.b;
                    }

                    if (v0 == this.a) {
                        return this.b;
                    }

                    if (this.c != null) {
                        arg6.removeSpan(this.c);
                        this.c = v2;
                    }

                    this.a = ((ClickableSpan) v2);
                    Selection.removeSelection(arg6);
                    return this.b;
                }

                if (this.a != null) {
                    if (this.c != null) {
                        arg6.removeSpan(this.c);
                        this.c = v2;
                    }

                    super.onTouchEvent(arg5, arg6, arg7);
                }

                this.a = ((ClickableSpan) v2);
                Selection.removeSelection(arg6);
            }

            label_29:
            return this.b;
        }
    }

    private int a;
    private com.ss.android.article.base.utils.b.a b;
    private WeakReference c;

    private LinkSpan(b arg2, com.ss.android.article.base.utils.b.a arg3, int arg4) {
        this(arg2, arg3, arg4, (byte) 0);
    }

    private LinkSpan(b arg2, com.ss.android.article.base.utils.b.a arg3, int arg4, byte arg5) {
        this.a = arg4;
        this.c = new WeakReference(arg2);
        this.b = arg3;
    }

    public static CharSequence processContentUrl(Context context, String arg11, b arg12) {
        int v2 = 0;
        if (TextUtils.isEmpty(((CharSequence) arg11))) {
            return "";
        }

        SpannableStringBuilder v0 = new SpannableStringBuilder();
        try {
            Matcher v3 = Pattern.compile("(http[s]?)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!$%^&*+?:_/=<>]*)?|[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.(com|cn|net|org|edu|gov|mil|int|biz|cc|club|tv|info|mobi|travel|pro|museum|coop|aero|hk|jp|tw|us|uk|ca|mo|de|fr|eu|br|au|at|ar|es|fi|iq|ir|in|il|ie|it|is|kr|kp|se|sg|th|za))+", 2).matcher(((CharSequence) arg11));
            SpannableString v1 = null;
            while (v3.find()) {
                int v4 = v3.start();
                v0.append(arg11.substring(v2, v4));
                v2 = v3.end();
                String v5 = arg11.substring(v4, v2);
                if (v5.matches("(http[s]?)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!$%^&*+?:_/=<>]*)?")) {
                    com.ss.android.article.base.utils.b.a v1_1 = new com.ss.android.article.base.utils.b.a(0, arg11.substring(v4, v2));
                    AppData.getInstance();
                    AppData.isNightMode();
                    v4 = context.getResources().getColor(com.ss.android.article.browser.R.color.link_text_color);
                    v5 = "| " + context.getString(com.ss.android.article.browser.R.string.webpage_link);
                    j v6 = new j(context, R.drawable.url_icon_dynamic);
                    LinkSpan v7 = new LinkSpan(arg12, v1_1, v4);
                    v1 = new SpannableString(v5);
                    v1.setSpan(v6, 0, 1, 33);
                    v1.setSpan(v7, 2, v5.length(), 33);
                } else if (v5.matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.(com|cn|net|org|edu|gov|mil|int|biz|cc|club|tv|info|mobi|travel|pro|museum|coop|aero|hk|jp|tw|us|uk|ca|mo|de|fr|eu|br|au|at|ar|es|fi|iq|ir|in|il|ie|it|is|kr|kp|se|sg|th|za))+")) {
                    com.ss.android.article.base.utils.b.a v6_1 = new com.ss.android.article.base.utils.b.a(1, arg11.substring(v4, v2));
                    AppData.getInstance();
                    AppData.isNightMode();
                    v4 = context.getResources().getColor(com.ss.android.article.browser.R.color.link_text_color);
                    v1 = new SpannableString(v5);
                    v1.setSpan(new LinkSpan(arg12, v6_1, v4), 0, v5.length(), 33);
                }

                if (v1 == null) {
                    continue;
                }

                v0.append(((CharSequence) v1));
            }

            v0.append(arg11.substring(v2, arg11.length()));
            SpannableStringBuilder v11 = v0;
        } catch (Exception v0_1) {
            Logger.e("LinkSpan", "exception occurs in processContentUrl, " + v0_1.getCause());
            v0_1.printStackTrace();
        }

        return arg11;
    }

    public final void onClick(View arg7) {
        if (this.c != null) {
            Object v0 = this.c.get();
            if (v0 != null) {
                com.ss.android.article.base.utils.b.a v1 = this.b;
                if (v1 != null && !TextUtils.isEmpty(v1.b)) {
                    switch (v1.a) {
                        case 0: {
                            ((b) v0).a(v1);
                            return;
                        }
                        case 1: {
                            Intent v2 = new Intent("android.intent.action.SEND");
                            v2.setType("text/html");
                            v2.putExtra("android.intent.extra.EMAIL", new String[]{v1.b});
                            ((b) v0).a.startActivity(Intent.createChooser(v2, "Send Email"));
                            return;
                        }
                    }

                    return;


                }
            }
        }
    }

    public final void updateDrawState(@NonNull TextPaint arg2) {
        if (this.a != 0) {
            arg2.setColor(this.a);
        }

        arg2.setUnderlineText(false);
    }
}

